package com.kuang.demo.controller;

import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bo.zzy.LogBo.ListPageLimit2;
import com.kuang.demo.bo.zzy.Role.*;
import com.kuang.demo.bo.zzy.TotalItem;
import com.kuang.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @RequestMapping("/options")
    public BaseRespVo zzy1(){
        List<ValueLabel> labels = roleService.selectAllRole();

        return BaseRespVo.ok(labels);
    }

    @RequestMapping("/list")
    public BaseRespVo zzy2(ListPageLimit2 listPageLimit){
        TotalItem totalItem = roleService.selectAllRoleByLimit(listPageLimit);

        return BaseRespVo.ok(totalItem);
    }

    @RequestMapping("/create")
    public BaseRespVo zzy3(@RequestBody NameDesc nameDesc){
        //首先判断是否已经存在
        int i = roleService.selectRoleName(nameDesc.getName());
        if(i != 0){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrmsg("该角色已经存在");
            baseRespVo.setErrno(640);
            return baseRespVo;
        }

        CreateRole createRole = roleService.createRole(nameDesc);
        return BaseRespVo.ok(createRole);
    }

    @RequestMapping("/update")
    public BaseRespVo zzy4(@RequestBody IdNaEnaAddUpDe idNaEnaAddUpDe){
        roleService.updateRole(idNaEnaAddUpDe);

        return BaseRespVo.ok();
    }

    @RequestMapping("/delete")
    public BaseRespVo zzy5(@RequestBody IdNaEnaAddUpDe idNaEnaAddUpDe){
        roleService.deleteRole(idNaEnaAddUpDe);


        return BaseRespVo.ok();
    }

    @PostMapping("/permissions")
    public BaseRespVo zzy6( @RequestBody RolePermission rolePermission){
            roleService.updatePermission(rolePermission);
            return BaseRespVo.ok();

    }

    @GetMapping("/permissions")
    public BaseRespVo zzy7(RolePermission rolePermission){
            ReturnPermission returnPermission = roleService.selectAllPermission(rolePermission.getRoleId());
            return BaseRespVo.ok(returnPermission);
    }


}
